<?php
namespace app\models;

use app\pays\CustomCommon;
use pqphp\base\Model;

class RechargeManager extends CustomCommon
{
	/*
	*统一调用充值接口
	*type :充值接口去读config名称
	*/
	public function Invok($sign,$type,$phone,$cardno,$money,$oldmoney,$pid)
	{
		try
		{
			$key="";
			$cardtype="";
			if(strlen($pid)>0){
				$model = new WebPayModel();
				$result = $model->finduserbypid($pid);
				$key=$result[0]['key'];
				$cardtype=$result[0]['cardtype'];
				$this->paylogs("获取商户key".$key);
			}
			if(isset($this->config['recharge'][$type]))
			{
				//获取充值卡号
				$url=$this->config['recharge'][$type];
				switch($cardtype)
				{
					case 'XINSUAN':
						//往卡上充值-微信支付并到芯算卡上充值 type==7是固定的，不要随意修改
						$param="cardno=$cardno&money=$money&oldmoney=$oldmoney&phone=$phone&type=7";
						$sign2=strtoupper(md5($param.$key));
						$rechargeUrl=$url.$param."&sign=".$sign2;
						$this->paylogs("充值接口:".$rechargeUrl);
						$result=$this->RequestUrl($rechargeUrl,"get");
						//exit($sign);
						if($result['code']==-1){
							$this->errormsg("手机号".$phone."卡号".$cardno."充值金额".$money."先钱账户金额".$oldmoney."充值接口充值报错：".$result['message']);
						}
						else if($result['code']==200){
							$this->paylogs("手机号".$phone."卡号".$cardno."充值金额".$money."先前账户金额".$oldmoney."充值成功");
						}
						break;
					default:
						$this->errormsg("手机号".$phone."卡号".$cardno."充值金额".$money."先前账户金额".$oldmoney."充值接口未进行逻辑处理");
						break;
				}
				$this->RechargeSuccess($sign,1);
			}
			else
			{
				$this->errormsg("手机号".$phone."卡号".$cardno."充值金额".$money."先钱账户金额".$oldmoney."未找到充值接口");
			}
		}
		catch(Exception $e)
		{
		}
	}


	/**
     * @无论任何支付，只要充值成功，就把字段设置成true
     * sign 操作唯一标识
	 * issuccess  是否充值成功
     */
	function RechargeSuccess($sign,$issuccess)
	{
		$data = array("issuccess" => $issuccess);
		$model=new Model();
		$model->settablename('payresult');
        $model->where(['sign = :sign'], [':sign' => $sign])->update($data);
	}
}